import style from './render/style'
import template from './render/template'
import Layout from './engine'

let __env = GameGlobal.wx || GameGlobal.tt || GameGlobal.swan;
let sharedCanvas = __env.getSharedCanvas();
let sharedContext = sharedCanvas.getContext('2d');

function draw(data, userData) {
    Layout.clear();
    let list = template(data, userData);
    Layout.init(list, style);
    Layout.layout(sharedContext);
}

function updateViewPort(data) {
    Layout.updateViewPort({
        x: data.x,
        y: data.y,
        width: data.width,
        height: data.height,
    });
}

function getFriendCloudStorage(key_v, callback) {
    __env.getUserInfo({
        openIdList: ['selfOpenId'],
        success: (userRes) => {
            console.log('success', userRes.data)
            let userData = userRes.data[0];
            //取出所有好友数据
            __env.getFriendCloudStorage({
                keyList: [key_v],
                success: res => {
                    console.log(" wx.getFriendCloudStorage success", res);
                    let dataRes = res.data;
                    dataRes.sort((a, b) => {
                        if (a.KVDataList.length == 0 && b.KVDataList.length == 0) {
                            return 0;
                        }
                        if (a.KVDataList.length == 0) {
                            return 1;
                        }
                        if (b.KVDataList.length == 0) {
                            return -1;
                        }
                        return b.KVDataList[0].value - a.KVDataList[0].value;
                    });
                    let friendData = [userData];
                    if (dataRes.length <= 0) {
                        friendData = [userData];
                    }
                    else {
                        friendData = dataRes;
                    }
                    if (callback) {
                        callback(friendData, userData);
                    }
                },
                fail: res => {
                    console.log("getFriendCloudStorage fail", res);
                },
            });
        },
        fail: (res) => {
            console.log("getUserInfo fail11111111", res);
        }
    });
}

function setFriendCloudStorage(key_v, value_v) {
    __env.getUserCloudStorage({
        // 以key/value形式存储
        keyList: [key_v],
        success: function (getres) {
            console.log('getUserCloudStorage', 'success', getres)
            if (getres.KVDataList.length != 0) {
                if (getres.KVDataList[0].value > value_v) {
                    return;
                }
            }
            // 对用户托管数据进行写数据操作
            __env.setUserCloudStorage({
                KVDataList: [{ key: key_v, value: "" + value_v }],
                success: function (res) {
                    console.log('setUserCloudStorage', 'success', res)
                },
                fail: function (res) {
                    console.log('setUserCloudStorage', 'fail')
                },
                complete: function (res) {
                    console.log('setUserCloudStorage', 'ok')
                }
            });
        },
        fail: function (res) {
            console.log('getUserCloudStorage', 'fail')
        },
        complete: function (res) {
            console.log('getUserCloudStorage', 'ok')
        }
    });
}

__env.onMessage(data => {
    console.log("data______________", data)
    if (data.type === 'engine' && data.event === 'viewport') {
        updateViewPort(data);
    }
    else {
        if (data.value == "getFriendData") {
            getFriendCloudStorage(data.strKey, (friendData, userData) => {
                draw(friendData, userData);
            });
        }
        else if (data.value == "setFriendData") {
            setFriendCloudStorage(data.strKey, data.score);
        }
    }
});
